home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / filutl34.zoo / fileutil.34 / src / statfs.c < prev   
Encoding:
C/C++ Source or Header  |  1993-02-06  |  1.8 KB  |  93 lines

  1. /*
  2.  * statfs() emulation for MiNT/TOS
  3.  *
  4.  * Written by Adrian Ashley (adrian@secret.uucp)
  5.  * and placed in the public domain.
  6.  */
  7.  
  8. #include <errno.h>
  9. #include <stat.h>
  10. #include <osbind.h>
  11. #include <unistd.h> /* for chdir, getcwd */
  12. #include <limits.h> /* for PATH_MAX */
  13. #ifdef __TURBOC__
  14. #include <sys\statfs.h>
  15. #else
  16. #include <sys/statfs.h>
  17. #endif
  18.  
  19. extern int __mint;
  20.  
  21. int statfs(path, buf)
  22.   char *path;
  23.   struct statfs *buf;
  24. {
  25.   int r;
  26.   _DISKINFO free;
  27.   struct stat statbuf;
  28.  
  29.   if (!buf || !path)
  30.   {
  31.     errno = EFAULT;
  32.     return -1;
  33.   }
  34.  
  35.   r = stat(path, &statbuf);
  36.  
  37.   if (r == -1)
  38.     return -1;
  39.  
  40.   if ((__mint >= 99) && (statbuf.st_dev >= 32))
  41.   {
  42.     /* Hack by HPP 02/06/1993: since MiNT 0.99 only returns     */
  43.     /* valid dfree info for pseudo-drives if they are the       */
  44.     /* current directory, change directories for the occasion.  */
  45.     char oldpath[PATH_MAX];
  46.     
  47.     if (getcwd(oldpath, PATH_MAX) != NULL)
  48.     {
  49.       chdir(path);
  50.       Dfree(&free, statbuf.st_dev + 1);
  51.       chdir(oldpath);
  52.     }
  53.     else
  54.       Dfree(&free, statbuf.st_dev + 1);
  55.   }
  56.   else
  57.     Dfree(&free, statbuf.st_dev + 1);
  58.  
  59.   buf->f_type = 0;
  60.   buf->f_bsize = free.b_secsiz * free.b_clsiz;
  61.   buf->f_blocks = free.b_total;
  62.   buf->f_bfree = buf->f_bavail = free.b_free;
  63.   buf->f_files = buf->f_ffree = buf->f_fsid.val[0] = buf->f_fsid.val[1] = -1L;
  64.  
  65.   return 0;
  66. }
  67.  
  68. #ifdef TEST
  69.  
  70. #include <stdio.h>
  71.  
  72. main(int argc, char **argv)
  73. {
  74.   int i = 0, r;
  75.   register char *p;
  76.   struct statfs stbuf;
  77.  
  78.   while (--argc)
  79.   {
  80.     p = argv[++i];
  81.  
  82.     r = statfs(p, &stbuf);
  83.     if (r == -1)
  84.       perror(p);
  85.     else
  86.       printf("statfs(`%s'): %ld free bytes\n", p,
  87.     (long)(stbuf.f_bfree * stbuf.f_bsize));
  88.   }
  89.   return 0;
  90. }
  91.  
  92. #endif
  93.